<template>
  <!-- 增改用户弹框 -->
  <hcDialog
    dialogWidth="30%"
    :dialogTitle="options.type === 'add' ? '新增工站' : '编辑工站'"
    :isVisable="options.show"
    @closeDialog="closeFun"
    @openDialog="openFun"
  >
    <el-form
      ref="formRef"
      :model="form"
      label-width="6vw"
      status-icon
      :rules="rules"
      :validate-on-rule-change="false"
    >
      <el-form-item prop="equipment_id" label="设备名称">
        <el-select v-model="form.equipment_id">
          <el-option
            v-for="item of equipArr"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="operation_id" label="工序名称">
        <el-select v-model="form.operation_id">
          <el-option
            v-for="item of operaArr"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="station_type" label="工站类型">
        <el-select v-model="form.station_type">
          <el-option
            v-for="item of typeArr"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="station_code" label="工站编号">
        <el-input v-model="form.station_code"> </el-input>
      </el-form-item>
      <el-form-item prop="station_name" label="工站名称">
        <el-input v-model="form.station_name"> </el-input>
      </el-form-item>
      <el-form-item prop="enabled" label="是否可用">
        <el-select v-model="form.enabled">
          <el-option label="可用" :value="true"></el-option>
          <el-option label="禁用" :value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <div class="btns">
          <el-button type="primary" @click="saveDialog()">{{
            t("common.saveText")
          }}</el-button>
          <el-button @click="closeFun">取消</el-button>
        </div>
      </el-form-item>
    </el-form>
  </hcDialog>
</template>
<script setup>
import { onMounted, reactive, ref } from "vue";
import { AddStation, updateStation } from "@/api/process";
import { getEquip, getOperation } from "@/api/process";
import { useI18n } from "vue-i18n";
import useDictionary from "@/hooks/useDictionary";
let { t } = useI18n();

const emit = defineEmits(["closeDialog"]);

const props = defineProps({
  options: {
    type: Object,
    default: () => {
      return {
        show: false,
        type: "add", // 用于判断是编辑还是删除 add edit
        data: null, // 当前编辑的用户
      };
    },
  },
});

const formRef = ref();
const form = reactive({
  id: 0,
  operation_id: "",
  equipment_id: "",
  station_type: "",
  station_code: "",
  station_name: "",
  enabled: true,
});
let equipArr = ref([]);
let operaArr = ref([]);
let typeArr = ref([]);

const rules = {
  operation_id: [{ required: true, message: "请选择工序", trigger: "change" }],
  equipment_id: [{ required: true, message: "请选择设备", trigger: "change" }],
  station_type: [{ required: true, message: "请选择类型", trigger: "change" }],
  station_code: [
    { required: true, message: "请输入工站编号", trigger: "blur" },
  ],
  station_name: [
    { required: true, message: "请输入工站名称", trigger: "blur" },
  ],
};

function saveDialog() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let res = "";
      if (props.options.type === "edit") {
        res = await updateStation(form);
      } else {
        res = await AddStation(form);
      }
      if (res) {
        ElMessage.success("保存成功");
        emit("closeDialog", true);
      } else {
        ElMessage.error("保存失败");
      }
    }
  });
}

// 关闭弹框
const closeFun = () => {
  formRef.value?.resetFields();
  emit("closeDialog");
};

function openFun() {
  if (props.options.type === "edit") {
    for (let key in form) {
      form[key] = props.options.data[key];
    }
  }
}

async function getAllEquip() {
  let res = await getEquip();
  equipArr.value =
    res.items
      ?.filter((item) => item.enabled)
      ?.map((item) => {
        return {
          label: item.equipment_name,
          value: item.id,
        };
      }) ?? [];
}
async function getAllOpera() {
  let res = await getOperation();
  operaArr.value =
    res.items
      ?.filter((item) => item.enabled)
      ?.map((item) => {
        return {
          label: item.operation_name,
          value: item.id,
        };
      }) ?? [];
}

function getData() {
  useDictionary({ dictType: "EMS_STATION_TYPE" }).then(
    (res) => (typeArr.value = res)
  );
}

onMounted(() => {
  getAllEquip();
  getAllOpera();
  getData();
});
</script>
<style lang="scss" scoped></style>
